#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QPainter>
#include <QMouseEvent>
#include <QDebug>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    buffer = QImage(400,400, QImage::Format_ARGB32);

    desenha();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::interfaceUpdate(void)
{
    ui->labelPrincipal->setPixmap(QPixmap::fromImage(buffer));
    update();
}

void MainWindow::desenha(void)
{
    QVector<QPoint> pontos;
    pontos.append(QPoint(0,0));
    pontos.append(QPoint(0,50));
    pontos.append(QPoint(50,50));
    pontos.append(QPoint(410,50));
    pontos.append(QPoint(200,200));

    interfaceUpdate();
}















































void MainWindow::mousePressEvent(QMouseEvent *event)
{
    pontoClicado = event->pos();
    event->accept();
}

void MainWindow::mouseReleaseEvent(QMouseEvent *event)
{
    if(pontoClicado == event->pos())
    {
        QPainter p(&buffer);
        p.drawLine(QPoint(0,0), ui->labelPrincipal->mapFrom(this, pontoClicado)  );
        p.end();
        interfaceUpdate();
    }
    event->accept();
}
